function Zfkhfx(){
	$("#orgyf").height($(window).height()/2-67);
	this.form=this.InitEleUI();
	$("#year").val($("#hidyear").val());
	$("#month").val($("#hidmonth").val());
	this.form.render('select');

	this.Template();
	this.InitOrg();
	//InitChart();
	this.GetStatisData();
	
	this.GetStatisYearDw();

}

Zfkhfx.prototype.InitEleUI=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
       
       // form.on('select(year)', function (data) {
    	//    $("#hidyear").val(data.value);
       // });
       //
       // form.on('select(template)', function (data) {
    	   // $("#hidmonth").val(data.value);
    	   // vZfkhfx.GetStatisData();
       // });

	   form.render();
	});  
	return form;
}

Zfkhfx.prototype.Rehref =function(){
    window.location.href="../lawass/law_kp_look";
}

Zfkhfx.prototype.InitOrg = function() {
	var comboboxm=$("#orgid").ligerComboBox({
		valueField : 'orgid',
		textField : 'orgname',
		selectBoxWidth : 250,
		selectBoxHeight:250,
		treeLeafOnly:false,
		valueFieldID:"hid",
		tree: {url : '../common/get_org_list?orgtype=0',
			checkbox: false, 
			nodeWidth:150,
			ajaxType: 'get', 
			idFieldName: 'orgid',
			textFieldName:"orgname",
			idField:"orgid",
        	parentIDField:"orgparentid"
		},
		onSelected : function(orgid, orgname) {// id为空值，name为选择的值
			$("#hidszdw").val(orgid);
			$("#hidorgname").val(orgname);
			vZfkhfx.GetStatisData();
			vZfkhfx.GetStatisYearDw();
		},
		width : 250
	});
	return comboboxm;
}

Zfkhfx.prototype.Template=function(){
    var names = [];
    $.ajax({
        type: "POST",
        url: "get_tempalte_list",
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');

            $("#template").find("option").remove();

            for (var i = 0; i < data.data.length; i++) {
                var element = data.data[i]

                $("#template").append("<option value=" + element.templateid + ">" + element.templatename + "</option>");

                names.push({id: element.templateid, name: element.dataname})
            }
            layui.use('form', function () {
                form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                form.on('select(template)', function (data) {
                    vZfkhfx.GetStatisData();
                });
                form.render();
                var node = $('.template');
                node.find('.layui-select-title').css('width', '300px');
            });

        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}


Zfkhfx.prototype.GetStatisData=function(){
	$("#orgname").html($("#hidorgname").val());
	var templateid = $("#template").val();
	$.ajax({
		type:"GET",
		url:"getzfkhdata",
		data:{templateid: templateid,szdw:$("#hidszdw").val()},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				if(data.items){
					if(data.items.monthmap){
						var monthobject=data.items.monthmap;
						var orgdata=[];
						var scoreata=[];
						var reddata=[];
                        var top = new Map();
						var minvalue=0.000;
					    var maxvalue=0.000;
						for(i=0;i<monthobject.length;i++){
							orgdata.push(monthobject[i].orgname.replace('派出所',''));
							var score3 = monthobject[i].score.toFixed(3);
							var total=(monthobject[i].redscore+monthobject[i].score).toFixed(3);
                            scoreata.push(score3);
                            reddata.push(monthobject[i].redscore.toFixed(3));
                            top.set(score3,monthobject[i].top);

							if(Number(total)>maxvalue){
							    maxvalue=total;
							}
							if(i==0){
								minvalue=score3;
							}
							else{
								if(Number(score3)<minvalue){
								  minvalue=score3;
								}
							}
						}
						InitChats(orgdata,scoreata,reddata,top,minvalue,maxvalue);
					}
					
					if(data.items.yearmap){
						var ymdata=[1,2,3,4,5,6,7,8,9,10,11,12];
						var yearobject=data.items.yearmap;
						var yeardata=[];
						for(l=0;l<ymdata.length;l++){
							var mvalue=0;
							if(yearobject.length>0){
								for(k=0;k<yearobject.length;k++){
									if(yearobject[k].monthstr==ymdata[l]){
										mvalue= yearobject[k].score.toFixed(3);
									}
								}
							}
							yeardata.push(mvalue);
						}
					}
					
					InitChatsOrg(yeardata);
				}
				
			}
			else{
				
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}




Zfkhfx.prototype.GetStatisYearDw=function(){
	$("#orgnameyear").html($("#hidorgname").val());
	$.ajax({
		type:"GET",
		url:"getzfkhyeardw",
		data:{szdw:$("#hidszdw").val()},
		beforeSend:function(XHR){

		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
					if(data.items.zfmapdw){
						var ymdata=[2015,2016,2017,2018,2019,2020,2021,2022];
						var ywbject=data.items.zfmapdw;
						var scoreata=[];
						var maxindex=0;
						for(l=0;l<ymdata.length;l++){
							var mvalue=-1;
							if(ywbject.length>0){
								for(k=0;k<ywbject.length;k++){
									if(ywbject[k].yearstr==ymdata[l]){
										mvalue=ywbject[k].rank;
										if(maxindex<mvalue){
											maxindex=mvalue;
										}
									}
								}
							}
							if(mvalue==-1){
								scoreata.push('');
							}
							else{
								scoreata.push(mvalue);
							}

						}
						InitChatsOrgYear(scoreata,maxindex);
					}
				}
			}
	})
}


var vZfkhfx;
$(function () {
	vZfkhfx = new Zfkhfx();
})




function InitChats(orgdata,scoreata,reddata,top,minvalue,maxvalue){
	$("#Chart").width($(window).width()-30);
	$("#Chart").height(($(window).height()/2-10));
	var myChart = echarts.init(document.getElementById('Chart'));
	option = {
		    tooltip : {
		        trigger: 'axis',
		        // axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		        //     type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        // }
                formatter: function(datas)
                {
                    var res = datas[0].name + '<br/>', val;
                    for(var i = 0, length = datas.length; i < length; i++) {
                    	val = datas[i].value;
                        res += datas[i].seriesName + '：' + val + '<br/>';
                    }
					var key = datas[0].value;
                    console.log(key);
                    res += '排名：'+top.get(key);
                    return res;
                }
		    },
		    legend: {
		        data: ['得分', '失分'],
		        x: 'right', // 'center' | 'left' | {number},
		        y: 'top',
		        padding: [20,10],
		        itemGap: 20,
		        textStyle: {
		            color: '#ffffff'                              // 图例文字颜色
		        }
		    },
		    grid: {
		        left: 10,
		        right: 20,
		        bottom: 15,
		        containLabel: true
		    },
		    xAxis:  {
		        type: 'category',
		        axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#aadeff'
                    },
                    interval:0,
                    rotate:320
                },
		        //data: ['周一','周二','周三','周四','周五','周六','周日']
		        data:orgdata,
		        axisTick:{
			    	show:false
			    },
			    axisLine:{
			    	show:false
			    }
		    },
		    yAxis: {
		        type: 'value',
		        axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#aadeff'
                    }
                },
                axisTick:{
			    	show:false
			    },
			    axisLine:{
			    	show:false
			    },
			    min:0,
		        max:maxvalue,
		        minInterval:0.1
			  // min:(minvalue-0.2).toFixed(3),
				//	max:maxvalue,
		       // minInterval:0.1
				//scale:true
		        
		    },
		    series: [
		        {
		            name: '得分',
		            type: 'bar',
		            stack: '总量',
		            label: {
		                normal: {
		                    show: true,
		                    position: 'insideRight'
		                }
		            },
		            itemStyle : {  
                        normal : {  
                        	 color: new echarts.graphic.LinearGradient(
                                     0, 0, 0, 1,
                                   [
                                     {offset: 0, color: '#fd8b66'},   // {offset: 0, color: '#2FDE80'},   
                                     {offset: 1, color: '#fa9492'}//  {offset: 1, color: '#2FDECA'}
                                   ])
  
                        }  
                    }, 
		            barWidth : 30,
		            data: scoreata
		        },
		        {
		            name: '失分',
		            type: 'bar',
		            stack: '总量',
		            label: {
		                normal: {
		                    show: true,
		                    position: 'insideRight'
		                }
		            },
		            itemStyle : {  
                        normal : {  
                        	 color: new echarts.graphic.LinearGradient(
                                     0, 0, 0, 1,
                                   [
                                      {offset: 0, color: '#42a8fc'},   // {offset: 0, color: '#2FDE80'},   
                                      {offset: 1, color: '#3d94fb'}//  {offset: 1, color: '#2FDECA'}
                                      
                                   ])
  
                        }  
                    }, 
		            barWidth : 30,
		            data: reddata
		        }
		    ]
		};
	myChart.setOption(option);
}


function InitChatsOrg(yeardata){
	
	//$("#ChartOrg").width($(window).width()-30);
	$("#ChartOrg").width($(window).width()/2-10);
	$("#ChartOrg").height(($(window).height()/2-115));
	var myChart = echarts.init(document.getElementById('ChartOrg'));
	option = {
			 tooltip : {
			        trigger: 'axis',
			        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
			            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
			        }
			    },
		    xAxis: {
		        type: 'category',
		        boundaryGap: true,
		        data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
		        axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#aadeff'
                    }
                },
                axisTick:{
			    	show:false
			    },
			    axisLine:{
			    	show:false
			    }
		    },
		    yAxis: {
		        type: 'value',
		        axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#aadeff'
                    }
                },
                axisTick:{
			    	show:false
			    },
			    axisLine:{
			    	show:false
			    },
			    min:0,
		        max:100,
		        minInterval:25
		    },
		    grid: {
		        left: 10,
		        right: 20,
		        bottom: 5,
		        x:10,
		        y:15,
		        x2:10,
		        y2:10,
		        containLabel: true
		    },
		    series: [{
		        data: yeardata,
		        type: 'line',
		        itemStyle : {  
                    normal : {  
                    	 color:'#00FF00',  
                         lineStyle:{  
                             color:'#825c8b'  
                         }    
                    }  
                },
                symbolSize: 8,//拐点大小
		        areaStyle: { normal : {  
		                        	 color: new echarts.graphic.LinearGradient(
		                                     0, 0, 0, 1,
		                                   [
		                                      {offset: 0, color: 'rgba(38,98,160,0.3)'},   // {offset: 0, color: '#2FDE80'},   
		                                      {offset: 1, color: 'rgba(250,250,250,0.3)'}//  {offset: 1, color: '#2FDECA'}
		                                      
		                                   ])
		  
		                        } 
		                        }
		    }]
		};
	
	myChart.setOption(option);
}





function InitChatsOrgYear(yeardata,maxindex){
	$("#ChartOrgYear").width($(window).width()-30);
	$("#ChartOrgYear").height(($(window).height()/2-110));
	var myChart = echarts.init(document.getElementById('ChartOrgYear'));
	option = {
			 tooltip : {
			        trigger: 'axis',
			        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
			            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
			        },
			        backgroundColor:'#3187c2',
			        textStyle: {
			              fontWeight: "normal",
			              color: "#aadeff",
			              fontSize: 8
			        },
			        formatter: function(params) {
	                    var result = '';
	                    params.forEach(function (item) {
	                    	//alert(JSON.stringify(item))
	                    	if(item.value>0){
	                    		result += "   "+item.marker +item.name+"年：     第 "+ item.value +" 名";
	                    	}

	                    });
	                    return result;
	                }

			    },



		    xAxis: { type: 'category',
		        boundaryGap: false,
		        data: ['2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022'],
		        axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#aadeff'
                    },
                    interval:0
                },
                axisTick:{
			    	show:false
			    },
			    axisLine:{
			    	show:false
			    }



		    },
		    yAxis: {
		    	inverse:true,
		    	type: 'value'
			        ,axisLabel: {
	                    show: true,
	                    textStyle: {
	                        color: '#aadeff'
	                    }
	                },
	                axisTick:{
				    	show:false
				    },
				    axisLine:{
				    	show:false,
				    	onZero: false
				    },
				    splitLine: {           // 分隔线
				        show: true,        // 默认显示，属性show控制显示与否
				        lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
				            color: ['#4170a8'],
				            width: 1,
				            type: 'solid'
				        }
				    },
				    splitArea: {           // 分隔区域
				        show: true,       // 默认不显示，属性show控制显示与否
				        areaStyle: {       // 属性areaStyle（详见areaStyle）控制区域样式
				            color: ['#305d94','none']
				        }
				    },
				    min:1,
			        max:(maxindex+1)%2==0?(maxindex+1):(maxindex+2)
			        ,minInterval:1
		    },
		    grid: {
		        left: 10,
		        right: 20,
		        bottom: 5,
		        x:0,
		        y:10,
		        x2:0,
		        containLabel: true
		    },
		    series: [{
		        data: yeardata,
		        type: 'line'
		        ,itemStyle : {
                    normal : {
                    	 color:'#00FF00',
                    	 borderColor:'red',
                         lineStyle:{
                             color:'#825c8b',
                             width:4
                         }

                    }
                },
                symbolSize: 12//拐点大小

		    }]
		};
	myChart.setOption(option);
}
















Zfkhfx.prototype.GetStatisDataNew=function(){
	$("#orgname").html($("#hidorgname").val());
	var templateid = $("#template").val();
	$.ajax({
		type:"GET",
		url:"getzfkhdata",
		data:{templateid: templateid,szdw:$("#hidszdw").val()},
		beforeSend:function(XHR){

		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				if(data.items){
					if(data.items.monthmap){
						var monthobject=data.items.monthmap;
						var orgdata=[];
						var scoreata=[];
						var reddata=[];
						var totaldata=[];
                        var top = new Map();
						var minvalue=0.000;
					    var maxvalue=0.000;
						for(i=0;i<monthobject.length;i++){
							orgdata.push(monthobject[i].orgname.replace('派出所',''));
							var score3 = monthobject[i].score.toFixed(3);
							var total=(monthobject[i].redscore+monthobject[i].score).toFixed(3);
                            scoreata.push(score3);
                            reddata.push(monthobject[i].redscore.toFixed(3));
							totaldata.push(total);
                            top.set(score3,monthobject[i].top);

							if(Number(total)>maxvalue){
							    maxvalue=total;
							}
							if(i==0){
								minvalue=score3;
							}
							else{
								if(Number(score3)<minvalue){
								  minvalue=score3;
								}
							}
						}
						InitChatsNew(orgdata,totaldata,scoreata,reddata,top,minvalue,maxvalue);
					}

					if(data.items.yearmap){
						var ymdata=[1,2,3,4,5,6,7,8,9,10,11,12];
						var yearobject=data.items.yearmap;
						var yeardata=[];
						for(l=0;l<ymdata.length;l++){
							var mvalue=0;
							if(yearobject.length>0){
								for(k=0;k<yearobject.length;k++){
									if(yearobject[k].monthstr==ymdata[l]){
										mvalue= yearobject[k].score.toFixed(3);
									}
								}
							}
							yeardata.push(mvalue);
						}
					}

					InitChatsOrg(yeardata);
				}

			}
			else{

			}
		},
		complete:function(XHR){

		},
		error:function(){

		}
	});
}








function InitChatsNew(orgdata,totalscore,scoreata,reddata,top,minvalue,maxvalue){
	$("#Chart").width($(window).width()-30);
	$("#Chart").height(($(window).height()/2-10));
	var myChart = echarts.init(document.getElementById('Chart'));
	option = {
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            crossStyle: {
                color: '#999'
            }
        }
    },
    toolbox: {
        feature: {
            dataView: {show: true, readOnly: false},
            magicType: {show: true, type: ['line', 'bar']},
            restore: {show: true},
            saveAsImage: {show: true}
        }
    },
    legend: {
        data:['总分','得分','失分']
    },
    xAxis: [
        {
            type: 'category',
            data: orgdata,
            axisPointer: {
                type: 'shadow'
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '总分&得分',
            min: 20,
            max: maxvalue,
            interval: 0.1,
            axisLabel: {
                formatter: '{value} ml'
            }
        },
        {
            type: 'value',
            name: '失分',
            min: 0,
            max: 10,
            interval: 1,
            axisLabel: {
                formatter: '{value} °C'
            }
        }
    ],
    series: [
        {
            name:'总分',
            type:'bar',
            data:totalscore
        },
        {
            name:'得分',
            type:'bar',
            data:scoreata
        },
        {
            name:'失分',
            type:'line',
            yAxisIndex: 1,
            data:reddata
        }
    ]
};
	myChart.setOption(option);
}